package com.kaibai.service.impl;

import com.kaibai.dao.CartDao;
import com.kaibai.dao.impl.CartDaoImpl;
import com.kaibai.pojo.CartItemDB;
import com.kaibai.service.CartService;
import com.kaibai.utils.JDBCUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class CartServiceImpl implements CartService {
    private CartDao cartDao = new CartDaoImpl();

    @Override
    public void addCartItemDB(CartItemDB cartItemDB) {
        Connection conn = null;
        try {
            conn = JDBCUtil.getConnection();
            conn.setAutoCommit(false);
            cartDao.addCartItemDB(conn,cartItemDB);
            conn.commit();
        } catch (SQLException throwables) {
            try {
                if (conn != null) {
                    conn.rollback();
                }
            } catch (SQLException t) {
                t.printStackTrace();
            }
            throwables.printStackTrace();

        }finally{
            JDBCUtil.close(conn,null);
        }
    }

    @Override
    public List<CartItemDB> getListByUid(Integer uid) {
        Connection conn = null;
        List<CartItemDB> list = null;
        try {
            conn = JDBCUtil.getConnection();
            conn.setAutoCommit(false);
            list = cartDao.getListByUid(conn,uid);
            conn.commit();
        } catch (SQLException throwables) {
            try {
                if (conn != null) {
                    conn.rollback();
                }
            } catch (SQLException t) {
                t.printStackTrace();
            }
            throwables.printStackTrace();
        }finally{
            JDBCUtil.close(conn,null);
        }
        return list;
    }

    @Override
    public boolean updateItemDB(int gid, int count,int uid) {
        Connection conn = null;
        boolean flag = true;
        try {
            conn = JDBCUtil.getConnection();
            conn.setAutoCommit(false);
            if(cartDao.updateItemDB(conn,gid,count,uid)!=1){
                flag=false;
            }
            conn.commit();
        } catch (SQLException throwables) {
            try {
                if (conn != null) {
                    conn.rollback();
                }
            } catch (SQLException t) {
                t.printStackTrace();
            }
            flag=false;
            throwables.printStackTrace();
        }finally{
            JDBCUtil.close(conn,null);
        }
        return flag;
    }

    @Override
    public CartItemDB existItemById(int gid,int uid) {
        Connection conn = null;
        CartItemDB cartItemDB = null;
        try {
            conn = JDBCUtil.getConnection();
            conn.setAutoCommit(false);
            cartItemDB = cartDao.existItemById(conn,gid,uid);
            conn.commit();
        } catch (SQLException throwables) {
            try {
                if (conn != null) {
                    conn.rollback();
                }
            } catch (SQLException t) {
                t.printStackTrace();
            }
            cartItemDB=null;
            throwables.printStackTrace();
        }finally{
            JDBCUtil.close(conn,null);
        }
        return cartItemDB;
    }

    @Override
    public boolean deleteItemDB(int gid, int uid) {
        Connection conn = null;
        boolean flag = true;
        try {
            conn = JDBCUtil.getConnection();
            conn.setAutoCommit(false);
            if(cartDao.deleteItemDB(conn,gid,uid)!=1){
                flag=false;
            }
            conn.commit();
        } catch (SQLException throwables) {
            try {
                if (conn != null) {
                    conn.rollback();
                }
            } catch (SQLException t) {
                t.printStackTrace();
            }
            flag=false;
            throwables.printStackTrace();
        }finally{
            JDBCUtil.close(conn,null);
        }
        return flag;
    }

    @Override
    public boolean deleteItemDBByUid(int uid) {
        Connection conn = null;
        boolean flag = true;
        try {
            conn = JDBCUtil.getConnection();
            conn.setAutoCommit(false);
            if(cartDao.deleteItemDBByUid(conn,uid)!=1){
                flag=false;
            }
            conn.commit();
        } catch (SQLException throwables) {
            try {
                if (conn != null) {
                    conn.rollback();
                }
            } catch (SQLException t) {
                t.printStackTrace();
            }
            flag=false;
            throwables.printStackTrace();
        }finally{
            JDBCUtil.close(conn,null);
        }
        return flag;
    }
}
